﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;

namespace IRUM.TR
{
    /// <summary>
    /// 야간선물 일봉
    /// </summary>
    public class t2805 : CBaseQuery
    {
        public event ChartDataHandler OnReceived;
        private List<ChartData> m_list = new List<ChartData>();

        private string focode = string.Empty;
        private string lastDBDate = string.Empty;
        private string date = string.Empty;
        private string cts_code = string.Empty;
        private string lastdate = string.Empty;
        private string lastDataDate = string.Empty;

        /// <summary>
        /// 야간선물 일봉
        /// </summary>
        /// <param name="focode">선물옵션코드, 8</param>
        /// <param name="lastDBDate">DB에 저장된 마지막 날짜</param>
        public t2805(string focode, string lastDBDate)
        {
            this.focode = focode;
            this.lastDBDate = lastDBDate;

            SetClassData(TR_Type.t2805);
        }

        public void SetOutBlockData(int Index)
        {
            lastDataDate = m_Query.GetFieldData(m_OutBlock1Name, "date", Index);

            if (Convert.ToInt32(lastDataDate) > Convert.ToInt32(lastDBDate))
            {
                ChartData cd = new ChartData();
                cd.shcode = "99999999"; //연결이므로 99999999 를 사용한다.
                cd.gubun = "D";
                cd.date = lastDataDate;
                cd.time = "151500";
                cd.open = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "open", Index));
                cd.high = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "high", Index));
                cd.low = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "low", Index));
                cd.close = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "close", Index));
                cd.jdiff_vol = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "volume", Index)); //거래량
                cd.openyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "openyak", Index));

                m_list.Add(cd);
            }
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            if (m_Query.IsNext == true && Convert.ToInt32(lastDataDate) > Convert.ToInt32(lastDBDate))
            {
                date = m_Query.GetFieldData(m_OutBlockName, "date", 0);
                cts_code = m_Query.GetFieldData(m_OutBlockName, "cts_code", 0);
                //lastdate = m_Query.GetFieldData(m_OutBlockName, "lastdate", 0);   //필요없음: 이걸 셋하면 오히려 더 적게 조회됨

                RequestNext();
            }
            else
            {
                if (OnReceived != null)
                {
                    OnReceived(this, new ChartDataArgs(m_list));
                }
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "shcode", 0, focode);         //선물옵션코드, 8
            m_Query.SetFieldData(m_InBlockName, "futcheck", 0, "1");          //선물최근월물, 1, 0:최근월물, 1:연결
            m_Query.SetFieldData(m_InBlockName, "date", 0, date);             //날짜, 8
            m_Query.SetFieldData(m_InBlockName, "cts_code", 0, cts_code);     //CTS종목코드, 8
            m_Query.SetFieldData(m_InBlockName, "lastdate", 0, "");           //전종목만기일, 8
            m_Query.SetFieldData(m_InBlockName, "cnt", 0, "500");             //조회건수, 3
        }
    }
}
